<template>
  <div class="login-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="login-form">
      <h2 class="login-title">顽石会员管理系统</h2>
      <el-form-item label="帐号" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('form')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {login, getUserInfo} from '@/api/login';

export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
      },

      rules: {
        username: [
          { required: true, message: "请输入用户姓名", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },

  components: {},

  methods: {

    async huoquInfo(token){
      const {data} = await getUserInfo(token);
                  //保存token 和 用户信息
                  localStorage.setItem('wanshi-mms-user', JSON.stringify(data))
                  this.$router.push("/");
    },

    submitForm(formName){
      this.$refs[formName].validate((valid) => {
          if (valid) {

            login(this.form.username,this.form.password).then(response =>{
                localStorage.setItem('wanshi-mms-token', response.data.token)
               this.huoquInfo(response.data.token);
               
            })

          } else {
            console.log('error submit!!');
            return false;
          }
        });
    }
  },
};
</script>

<style scoped>
.login-form {
  width: 350px;
  /* 上下间隙 160px, 左右自动居中 */
  margin: 160px auto;
  background-color: rgb(255, 255, 255, 0.8);
  padding: 28px;
  border-radius: 20px;
}
.login-container {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url("../../assets/login.jpg");
}
.login-title {
  color: #303133;
  text-align: center;
}
</style>